package com.lizardmind.haofudoctor.ui.aboutMe

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.linxiao.framework.activity.BaseActivity
import com.linxiao.framework.adapter.BaseAdapter
import com.linxiao.framework.util.getDataBinding
import com.linxiao.framework.util.toasts
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.databinding.ActivityCollectBinding
import com.lizardmind.haofudoctor.databinding.ItemCollectBinding
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.other.DoctorInfo
import com.lizardmind.haofudoctor.other.MyCollectBean
import com.lizardmind.haofudoctor.other.Request
import io.reactivex.functions.Action
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_collect.*
import kotlinx.android.synthetic.main.include_title_text.*
import org.jetbrains.anko.sdk25.coroutines.onClick

//收藏activity
class CollectActivity : BaseActivity() {

    val list= arrayListOf<MyCollectBean>()
    private var page=1
    private val count=10

    val adapter by lazy { BaseAdapter<MyCollectBean,ItemCollectBinding>(R.layout.item_collect,list) }

    val binding by lazy { getDataBinding<ActivityCollectBinding>(R.layout.activity_collect) }
    override fun onCreatRootView(): View? {
        return binding.root
    }

    override fun onInitView(savedInstanceState: Bundle?) {
        title.apply {
            titleText.text="收藏"
            imgLeft.setImageResource(R.mipmap.back)
            imgLeft.setOnClickListener{finish()}
        }
        imgRight.setImageResource(R.mipmap.search)
        imgRight.onClick {
            toasts("搜索药方")
        }

        rvCollect.layoutManager=LinearLayoutManager(this)
        rvCollect.adapter=adapter
        adapter.onBind { itemBingding, position, data ->
            itemBingding.tvCancelCollect.setOnClickListener {
                cancelCollect(position,"1")
            }
            itemBingding.tvShare.setOnClickListener {
                toasts("分享")
            }
        }

        getMyCollect()

        refreshCollect.setOnRefreshListener(object : RefreshListenerAdapter(){
            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                page=1
                list.clear()
                getMyCollect(1)
            }
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                page++
                getMyCollect(2)
            }
        })
    }


    //加载我的收藏 0=自启动刷新，1=刷新，2=加载
    private fun getMyCollect(flag: Int =0){
        if (flag==0){ refreshCollect.startRefresh() }
        postApi<Request<List<MyCollectBean>>>(hashMapOf("action" to "collection",
                "token" to DoctorInfo.token,
                "type" to "1",
                "p" to page.toString(),
                "count" to count.toString()),
                Consumer {
                    it.dispose(notData = {
                    },data = {
                        list.addAll(it)
                        adapter.notifyDataSetChanged()
                    })
                }, Action {
            if (flag==1 || flag==0){
                refreshCollect.finishRefreshing()
            }else if (flag==2){
                refreshCollect.finishLoadmore()
            }
        })
    }

    //取消收藏
    private fun cancelCollect(position:Int,type:String){
        postApi<Request<String>>(
                hashMapOf("action" to "cancel_collection",
                        "token" to DoctorInfo.token,
                        "id" to list[position].art_id,
                        "type" to type),
                Consumer {
                    it.dispose{
                        data: String ->
                        list.removeAt(position)
                        adapter.notifyDataSetChanged()
                        toasts(data)
                    }
                },Action {

        },context = this)
    }


    fun add(id:Int){
        postApi<Request<String>>(hashMapOf("action" to "add_collection",
                "token" to DoctorInfo.token,
                "id" to id.toString(),
                "type" to "1"),
                Consumer {
                }, Action {

        })
    }

}
